package com.abhigarde.personalexpenses;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.abhigarde.personalexpenses.model.Expense;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class PersonalExpensesAndroidActivity extends Activity {
	//Need to provide the complete url and not localhost.
	// Else it will give out a connection problem.
	 String listUrl = "http://192.168.1.11:8080/ReportingEngine/expenseservice/listexpense";
	 String addUrl = "http://192.168.1.11:8080/ReportingEngine/expenseservice/addexpense";
	 String deleteUrl = "http://192.168.1.11:8080/ReportingEngine/expenseservice/deleteexpense";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InputStream source = retrieveStream(listUrl);
        Reader reader = new InputStreamReader(source);
        
        String myString = null;
		try {
			myString = IOUtils.toString(source, "UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray Jarray = parser.parse(myString).getAsJsonArray();
        ArrayList<Expense> expenses = new ArrayList<Expense>();
        
        
        for(JsonElement obj : Jarray )
        {
            Expense expense = gson.fromJson( obj , Expense.class);
            expenses.add(expense);
        }
        
        for(int i=0;i<expenses.size();i++){
        	Toast.makeText(this, expenses.get(i).getExpenseName(), Toast.LENGTH_SHORT).show();
        	Log.w("Expense number "+i+" is : ",expenses.get(i).getExpenseName());
        }      
    }
    
    private InputStream retrieveStream(String url) {
        
        DefaultHttpClient client = new DefaultHttpClient(); 
        
        HttpGet getRequest = new HttpGet(url);
          
        try {
           
           HttpResponse getResponse = client.execute(getRequest);
           final int statusCode = getResponse.getStatusLine().getStatusCode();
           System.out.println("Status code "+statusCode);
           if (statusCode != HttpStatus.SC_OK) { 
              Log.w(getClass().getSimpleName(), 
                  "Error " + statusCode + " for URL " + url); 
              return null;
           }

           HttpEntity getResponseEntity = getResponse.getEntity();
           return getResponseEntity.getContent();
           
        } 
        catch (Exception e) {
        	e.printStackTrace();
           getRequest.abort();
           Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
        }
        
        return null;
        
     }
    
    

}